package com.example.bbs.dao.impl;

import com.example.bbs.dao.BaseDao;
import com.example.bbs.dao.BlockDao;
import com.example.bbs.dao.CallBack;
import com.example.bbs.model.Block;
import com.example.bbs.utils.TimeUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther: zhangxin
 * @Description:
 */
public class BlockDaoImpl extends BaseDao implements BlockDao, CallBack {

    @Override
    public List<Block> getBlockAll() {

        String sql = "select * from block";
        List<Object> lists = executeQuery(this,sql);
        return (List<Block>)(Object)lists;
    }

    @Override
    public Block getBlockById(int bid) {

        String sql = "select * from block where block_id = ?";
        List<Object> lists = executeQuery(this,sql,bid);
        if(lists.size() == 0 || lists == null){
            return null;
        }
        return (Block)(Object)lists.get(0);
    }

    @Override
    public Boolean updateBlockPostNumber(int bid) {
        String sql = "update block set post_number = post_number + 1 where block_id = ?";

        return executeUpdate(sql,bid);
    }


    @Override
    public List<Object> doResultSet(ResultSet resultSet) {
        List<Object> lists = new ArrayList<>();
        Block block = null;
        while (true){
            try {
                if (!resultSet.next()) break;
                block = new Block(resultSet.getInt(1), resultSet.getString(2), resultSet.getInt(3),TimeUtil.getStringToDate(resultSet.getString(4)));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            lists.add(block);
        }
        return lists;
    }
}
